package com.bee.bootstrap.system.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.bee.bootstrap.system.entity.Dictionary;
import com.bee.bootstrap.system.service.IDictionaryService;
import com.bee.common.core.jsonview.PrivilegeView;
import com.bee.common.core.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liekkas
 * @since 2018-04-13
 */
@RestController
@RequestMapping("/dictionarys")
@Api(value = "/dictionarys", description = "数据字典管理")
public class DictionaryController {

    @Autowired
    private IDictionaryService dictionaryServiceImpl;

    @PostMapping("")
    @ApiOperation(value="添加数据字典")
    public Result addDictionary(@Valid @RequestBody Dictionary dictionary, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return Result.fail(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        return dictionaryServiceImpl.addDictionary(dictionary);
    }

    @PutMapping("/{id}")
    @ApiOperation(value="修改数据字典")
    public Result updateDictionary(@Valid @RequestBody Dictionary dictionary, Errors bindingResult, @PathVariable(name="id") Long id){
        if(bindingResult.hasErrors()){
            return Result.fail(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        return dictionaryServiceImpl.updateDictionary(dictionary);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value="删除数据字典")
    public Result deleteDictionary(@PathVariable(name="id") Long id){
        return dictionaryServiceImpl.deleteDictionary(id);
    }

    @GetMapping("/{id}")
    @ApiOperation(value="根据Id获取数据字典")
    public Result readDictionary(@PathVariable(name="id") Long id){
        return Result.success(dictionaryServiceImpl.selectById(id));
    }

    @GetMapping("")
    @ApiOperation(value="获取数据字典分页对象")
    @PrivilegeView
    public Result findDictionarys(@ModelAttribute Dictionary dictionary,Page<Dictionary> page){
        return dictionaryServiceImpl.findPageView(page,dictionary);
    }

    @GetMapping("/tree")
    @ApiOperation(value="获取数据字典树形数据", notes="获取数据字典树形数据")
    public Result findDictionarysTree(){
        return dictionaryServiceImpl.selectTreeView();
    }


    //枚举类型统一放置在package com.bee.common.core.enums
    @GetMapping("/enum")
    @ApiOperation(value="获取枚举类型数据接口", notes="获取枚举类型数据接口")
    public Result findEnums(@RequestParam("type") String enums){
        try {
            Class<?> enumClass =Class.forName("com.bee.common.core.enums." + enums);
            Object[] objs = enumClass.getEnumConstants();
            return Result.success(objs);
        } catch (Exception e) {
            return Result.fail("未找到对应枚举类");
        }
    }


}
